#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Osh.Media
{
	public abstract class MediaBase : IMedia
	{
		private IMediaLibrary _library;
		private Uri _uri;
		private string _name;
		private MediaAttributeCollection _attributes;

		protected MediaBase()
		{
		}

		public MediaBase(IMediaLibrary library, Uri uri)
		{
			if(library == null)
				throw new ArgumentNullException("library");
			if(uri == null)
				throw new ArgumentNullException("uri");
			_uri = uri;
			_library = library;
		}

		#region IMedia
		public virtual IMediaLibrary Library
		{
			get { return _library; }
			protected set { _library = value; }
		}

		public virtual Uri Uri
		{
			get { return _uri; }
			protected set { _uri = value; }
		}

		public virtual string Name
		{
			get { return _name; }
			protected set { _name = value; }
		}

		public MediaAttributeCollection Attributes
		{
			get
			{
				if(_attributes == null)
					_attributes = new MediaAttributeCollection();
				return _attributes;
			}
		}
		#endregion IMedia

		/// <summary>
		/// Returns this media uri in string form accessable from scripts.
		/// </summary>
		public string UriString
		{
			get { return this.Uri.ToString(); }
		}

		public virtual string Description
		{
			get { return this.Attributes[MediaAttributeNames.Description] as string; }
			protected set { this.Attributes[MediaAttributeNames.Description] = value; }
		}

		public static Uri ParseMediaUri(string mediaUri)
		{
			if(string.IsNullOrEmpty(mediaUri))
				throw new ArgumentNullException("mediaUri");

			Uri uri;
			if(!Uri.TryCreate(mediaUri, UriKind.Absolute, out uri))
				throw new ArgumentOutOfRangeException("mediaUri", mediaUri, "Invalid media Uri format.");

			return uri;
		}
	}
}
